package com.kasax.plugin.app.visitor.addLog

import com.kasax.plugin.app.LogUtils
import org.objectweb.asm.ClassVisitor
import org.objectweb.asm.MethodVisitor
import org.objectweb.asm.Opcodes

class AddLogClassVisitor extends ClassVisitor {
    String TAG = "AddLogClassVisitor"
    String className = ""

    AddLogClassVisitor(ClassVisitor classVisitor) {
        super(Opcodes.ASM7, classVisitor)
    }

    @Override
    void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
        super.visit(version, access, name, signature, superName, interfaces)
        this.className = name
        LogUtils.d(TAG, "visit className: $className")
    }

    @Override
    MethodVisitor visitMethod(int access, String name, String descriptor, String signature, String[] exceptions) {
        LogUtils.d(TAG, "visit visitMethod: $className , method-name: $name")
        def mv = super.visitMethod(access, name, descriptor, signature, exceptions)
        // 判断当前类是否为 AppCompatActivity 的子类
        if (className.endsWith("Activity") && name != "<init>") {
            if (name == "onCreate") {
                return new AdddLogInitMethodVisitor(mv)
            } else {
                return new AdddLogMethodVisitor(mv)
            }
        }
        return mv
    }
}